package com.chuanyekeji.aokai.Controller;

import com.chuanyekeji.aokai.Config.ReadPathConfig;
import com.chuanyekeji.aokai.Entity.FirstPage;
import com.chuanyekeji.aokai.Entity.ProductType;
import com.chuanyekeji.aokai.Mapper.FirstPageMapper;
import com.chuanyekeji.aokai.Mapper.ProductTypeMapper;
import com.chuanyekeji.aokai.Util.FileUtil;
import com.chuanyekeji.aokai.Util.ImageCodeUtil;
import com.chuanyekeji.aokai.Util.ReturnData;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
public class FirstPageController {

    @Autowired
    ReadPathConfig readPathConfig;

    @Autowired
    FirstPageMapper firstPageMapper;

    @Autowired
    ProductTypeMapper productTypeMapper;

    /**
     * 获取验证码图片
     * @return
     */
    @RequestMapping("getImageCode")
    String getImageCode() {
        //封装返回数据
        Map<String,Object> data = new HashMap<>();
        Gson gson = new Gson();
        //获取根目录
        String firstImagePath = "";
        try {
            firstImagePath = ResourceUtils.getURL("classpath:").getPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //获取文件上传时的项目相对路径
        File file = new File(firstImagePath,"static/" + readPathConfig.getRandom());
        firstImagePath = file.getAbsolutePath() + "/";
        ImageCodeUtil imageCodeUtil = new ImageCodeUtil();
        String imageName = imageCodeUtil.getRandcode(firstImagePath);
        data.put("code",0);
        data.put("url",readPathConfig.getFirstPage() + readPathConfig.getRandom() + "/" + imageName + ".jpg");
        data.put("value",imageName);
        return ReturnData.success("success",gson.toJson(data));
    }

    /**
     * 获取轮转图
     * @return
     */
    @RequestMapping("index_get_banner")
    String index_get_banner() {
        List<FirstPage> firstPageList = firstPageMapper.getFirstPageByAll();
        return ReturnData.success("success",new Gson().toJson(firstPageList));
    }

    /**
     * 保存首页轮转图
     * @param images
     * @return
     */
    @RequestMapping("setBanner")
    String setBanner(@RequestParam(value = "image",required = false) MultipartFile[] images) {

        //封装返回数据
        Map<String,Object> data = new HashMap<>();
        Gson gson = new Gson();
        //获取根目录
        String firstImagePath = "";
        try {
            firstImagePath = ResourceUtils.getURL("classpath:").getPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //获取文件上传时的项目相对路径
        File file = new File(firstImagePath,"static/" + readPathConfig.getImage());
        firstImagePath = file.getAbsolutePath() + "/";

        for(int i=0;i<images.length;i++) {
            String fileName = images[i].getOriginalFilename();
            //随机码
            String imgCode = UUID.randomUUID().toString();
            //获取文件后缀名
            String[] str = fileName.split("\\.");
            String imageName = imgCode + "." + str[1];
            try {
                //保存图片
                if(FileUtil.uploadFile(images[i].getBytes(), firstImagePath,imageName)) {
                    data.put("code",0);
                    data.put("url",imageName);
                    FirstPage f = new FirstPage();
                    f.setIsEnable(1);
                    f.setFirstPageImage(readPathConfig.getImage() + "/" + imageName);
                    firstPageMapper.insertSelective(f);
                    while(firstPageMapper.countEnable() > 5) {
                        firstPageMapper.delete();
                    }
                } else {
                    data.put("code",1);
                    data.put("message","error");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return gson.toJson(data);
    }

    /**
     * 修改商品类别
     * @param id
     * @param text
     * @return
     */
    @RequestMapping("editProductType")
    String editProductType(int id,String text) {
        if(text.indexOf("-") == -1) {
            return ReturnData.error(9950,"请输入英文标签");
        }
        String[] names = text.split("-");
        ProductType productType = productTypeMapper.selectByPrimaryKey(id);
        productType.setProductTypeName(names[0]);
        productType.setProductTypeName_en(names[1]);
        productTypeMapper.updateByPrimaryKeySelective(productType);
        return ReturnData.success("success");
    }
}
